/*
 * utils.cpp
 *
 *  Created on: Nov 17, 2012
 *      Author: monitz
 */

#include "../include/utils.h"

using namespace std;

void utils::log(stringstream &sstm)
{
	ofstream fh;
	fh.open("random.log",ios::app);
	if(fh.is_open())
	{
	fh << sstm.str();
	}
	fh.close();
}

utils::utils():CSmaxSemesters(0),PGmaxSemesters(0),CSstudentsCounter(0),PGstudentsCounter(0),malagStudents()
{
}

vector<Student*> utils::parseStudents(int PGtodo,int CStodo,bool malag) {
	vector<Student*> ans;
	string line, path, dep, img;
	int id;
	ifstream fh("students.conf");
	stringstream sstrm;
	if (fh.is_open()) {
		while (fh.good()) {
			getline(fh, line);
			//read the student id
			id = atoi(line.substr(0, line.find_first_of(',')).c_str());
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//read the student dep
			dep = line.substr(0, line.find_first_of(','));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//read the student img
			img = line.substr(0, line.find_first_of(','));
			//create student object
			if (dep == "CS") {
				CSstudentsCounter++;
				ans.push_back(new CSStudent(id, img, CStodo));
			} else {
				PGstudentsCounter++;
				if(!malag)
				{
					ans.push_back(new PGStudent(id, img, PGtodo));
				}
				else	//if malag is true, then dont sign students to uni, but put them in quarantine at malagStudents vector
				{
					sstrm << id << " is being denied his education." << endl;
					malagStudents.push_back(new PGStudent(id, img, PGtodo));
				}
			}
		}
		log(sstrm);
		fh.close();
	} else
		cout << "Unable to open file students.conf" << endl;
	return ans;
}

vector<vector<Course*> > utils::parseCourses(bool malag)
{
	vector<vector<Course*> > ans;
	ans.resize(2);
	string line, dep, name;
	int sem, min;
	ifstream fh("courses.conf");
	if (fh.is_open()) {
		while (fh.good()) {
			getline(fh, line);
			//read the department
			dep = line.substr(0, line.find_first_of(','));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//read the name
			name = line.substr(0, line.find_first_of(','));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//read semester number
			sem = atoi(line.substr(0, line.find_first_of(',')).c_str());
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//read minimum grade
			min = atoi(line.c_str());
			if (dep == "ELECTIVE") {
				ans.at(sem % 2).push_back(new ELCourse(name, min, sem));
			} else {
				if (dep == "CS") {
					ans.at(sem % 2).push_back(new CSCourse(name, min, sem));
					if(sem>CSmaxSemesters)
						CSmaxSemesters=sem;
				} else {
					if(!malag)
					{
						ans.at(sem % 2).push_back(new PGCourse(name, min, sem));
						if(sem>PGmaxSemesters)
						PGmaxSemesters=sem;
					}
				}
			}
		}
		//if malag answer is no, make sure PG students dont graduate by make maxSemester unreachable
		 if(malag)
		 	 PGmaxSemesters=3;
		fh.close();
	} else
		cout << "Unable to open file courses.conf" << endl;
	return ans;
}

vector<int> utils::parseCurric() {
	vector<int> ans;
	ans.resize(3);
	string line;
	ifstream fh("curriculum.conf");
	if (fh.is_open()) {
			getline(fh, line);
			//read the number of semesters

			ans.at(0) = atoi(line.substr(line.find_first_of('=')+1,-1).c_str());
			//parse number of electives
			while(fh.good())
			{
				getline(fh, line);
			if(line.substr(0, line.find_first_of(','))=="CS")
			{
				ans.at(2)=atoi(line.substr(line.find_first_of(',')+1,-1).c_str());
			}
			else
			{
				ans.at(1)=atoi(line.substr(line.find_first_of(',')+1,-1).c_str());
			}
			}
						fh.close();
	} else
		cout << "Unable to open file curriculum.conf" << endl;

	return ans;
}

bool compareStudeID(Student* i, Student* j)
{
	return (i->getID()) < (j->getID());
}

void utils::showUniGraduation(vector <Student*> students)
{
	sort(students.begin(),students.end(),compareStudeID);
	std::vector<Student*>::iterator i=students.begin();
	if(*i!=NULL)	//if there are students in vector
	{
		std::string dep;
		int CSnumOfStudents=0;
		int PGnumOfStudents=0;
		bool studentPass;
		//create imageOperation
		ImageOperations opr;
		//student image skeleton
		ImageLoader	imageOutline(200,100);
		//create frames for each department
		ImageLoader CSframe=ImageLoader(imageOutline.getImage().size().height,imageOutline.getImage().size().width*CSstudentsCounter);
		ImageLoader PGframe=ImageLoader(imageOutline.getImage().size().height,imageOutline.getImage().size().width*PGstudentsCounter);

		//insert students images to frames
		for(;i!=students.end();i++)
		{
			stringstream sstrm;
			dep=(*i)->getDepartment();
			ImageLoader img((*i)->getImage());
			opr.resize(img.getImage(),imageOutline.getImage());		//make image default size
			img=imageOutline;
			//placing images at right department frame
			if(dep=="CS")
			{
				CSnumOfStudents++;
				studentPass=studentGraduation(CSframe,*(*i),CSmaxSemesters,imageOutline.getImage().size().width*(CSnumOfStudents-1));
			}
			else if(dep=="PG")
			{
				PGnumOfStudents++;
				studentPass=studentGraduation(PGframe,*(*i),PGmaxSemesters,imageOutline.getImage().size().width*(PGnumOfStudents-1));
			}

			if(studentPass)
				sstrm << (*i)->getID()<< " has graduated" << "(" << (*i)->getSem() << ")"<< endl;
			else
				sstrm << (*i)->getID() << " has not graduated" << "(" << (*i)->getSem() << ")"<< endl;
			log(sstrm);
		}

		if(!malagStudents.empty())		//there are students denied by malag
		{
			sort(malagStudents.begin(),malagStudents.end(),compareStudeID);
			PGnumOfStudents=0;
			i=malagStudents.begin();
			for(;i!=malagStudents.end();i++)
			{
				PGnumOfStudents++;
				studentPass=studentGraduation(PGframe,*(*i),PGmaxSemesters,imageOutline.getImage().size().width*(PGnumOfStudents-1));
			}
		}

		CSframe.displayImage();
		PGframe.displayImage();
		CSframe.saveImage("CS.jpg");
		PGframe.saveImage("PG.jpg");
	}

}

bool utils::studentGraduation(ImageLoader& frame, Student& s,int depMaxSemesters, int xLocation)
{
	bool studentPass=true;
	ImageOperations opr;
	ImageLoader	imageOutline(200,100);
	ImageLoader img(s.getImage());
	opr.resize(img.getImage(),imageOutline.getImage());		//make image default size
	img=imageOutline;
	if(!s.isDegreeGraduate(depMaxSemesters)) 	// if student didnt grauate make photo grey
	{
		studentPass=false;
		opr.rgb_to_greyscale(img.getImage(),img.getImage());
	}
	//move to frame
	opr.copy_paste_image(img.getImage(),frame.getImage(),xLocation);
	return studentPass;
}

utils::~utils() {
}
